<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="酒店名称" prop="hotelName">
        <el-input
          v-model="queryParams.hotelName"
          placeholder="请输入酒店名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="酒店地址" prop="hotelAddress">
        <el-input
          v-model="queryParams.hotelAddress"
          placeholder="请输入酒店地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="酒店星级" prop="hotelType">
        <el-select v-model="queryParams.hotelType" placeholder="请选择酒店星级" clearable>
          <el-option
            v-for="dict in dict.type.hotel_star"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属景区" prop="areaId">
        <el-select
          v-model="queryParams.areaId"
          placeholder="请输入所属景区"
          clearable
          @keyup.enter.native="handleQuery">
          <el-option
            v-for="area in scenicAreas"
            :key="area.areaId"
            :label="`${area.areaId} - ${area.areaName}`"
            :value="area.areaId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="酒店一间房的价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入酒店一间房的价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="含早餐" prop="breakfast">
        <el-select v-model="queryParams.breakfast" placeholder="请选择含早餐" clearable>
          <el-option
            v-for="dict in dict.type.service_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="接机服务" prop="pickup">
        <el-select v-model="queryParams.pickup" placeholder="请选择接机服务" clearable>
          <el-option
            v-for="dict in dict.type.service_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['booking:hotel:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['booking:hotel:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['booking:hotel:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['booking:hotel:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hotelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="酒店id" align="center" prop="hotelId" />
      <el-table-column label="酒店名称" align="center" prop="hotelName" />
      <el-table-column label="酒店地址" align="center" prop="hotelAddress" />
      <el-table-column label="酒店星级" align="center" prop="hotelType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hotel_star" :value="scope.row.hotelType"/>
        </template>
      </el-table-column>
      <el-table-column label="所属景区" align="center" prop="areaId" />
      <el-table-column label="酒店一间房的价格" align="center" prop="price" />
      <el-table-column label="含早餐" align="center" prop="breakfast">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.service_yes_no" :value="scope.row.breakfast"/>
        </template>
      </el-table-column>
      <el-table-column label="接机服务" align="center" prop="pickup">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.service_yes_no" :value="scope.row.pickup"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['booking:hotel:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['booking:hotel:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-home"
            @click="handleBook(scope.row)"
            v-hasPermi="['booking:book:add']"
          >预订</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改酒店信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="酒店名称" prop="hotelName">
          <el-input v-model="form.hotelName" placeholder="请输入酒店名称" />
        </el-form-item>
        <el-form-item label="酒店地址" prop="hotelAddress">
          <el-input v-model="form.hotelAddress" placeholder="请输入酒店地址" />
        </el-form-item>
        <el-form-item label="酒店星级" prop="hotelType">
          <el-select v-model="form.hotelType" placeholder="请选择酒店星级">
            <el-option
              v-for="dict in dict.type.hotel_star"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属景区" prop="areaId">
          <el-select v-model="form.areaId" placeholder="请选择所属景区" >
            <el-option
              v-for="area in scenicAreas"
              :key="area.areaId"
              :label="`${area.areaId} - ${area.areaName}`"
              :value="area.areaId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="酒店一间房的价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入酒店一间房的价格" />
        </el-form-item>
        <el-form-item label="含早餐" prop="breakfast">
          <el-select v-model="form.breakfast" placeholder="请选择含早餐">
            <el-option
              v-for="dict in dict.type.service_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="接机服务" prop="pickup">
          <el-select v-model="form.pickup" placeholder="请选择接机服务">
            <el-option
              v-for="dict in dict.type.service_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 预订酒店信息对话框 -->
    <el-dialog :title="title" :visible.sync="openBook" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="hotelbookrules" label-width="80px">
        <el-form-item label="用户id" prop="userId" >
          <el-input v-model="form.userId" placeholder="请输入用户id" :disabled=true />
        </el-form-item>
        <el-form-item label="酒店id" prop="hotelId" >
          <el-input v-model="form.hotelId" placeholder="请输入酒店id" :disabled=true />
        </el-form-item>
        <el-form-item label="入住日期" prop="checkinDate">
          <el-date-picker clearable
                          v-model="form.checkinDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择入住日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="退房日期" prop="endDate">
          <el-date-picker clearable
                          v-model="form.endDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择退房日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="人数" prop="guestCount">
          <el-input v-model="form.guestCount" placeholder="请输入人数" />
        </el-form-item>
        <el-form-item label="需求房间数量" prop="roomCount">
          <el-input v-model="form.roomCount" placeholder="请输入需求房间数量" @input="calculateTotalPrice()"/>
        </el-form-item>
        <el-form-item label="总价" prop="totalPrice">
          <el-input v-model="form.totalPrice" placeholder="请输入总价" :disabled=true />
        </el-form-item>
<!--        <el-form-item label="支付状态" prop="paymentStatus">-->
<!--          <el-radio-group v-model="form.paymentStatus"  >-->
<!--            <el-radio-->
<!--              v-for="dict in dict.type.payment_status"-->
<!--              :key="dict.value"-->
<!--              :label="dict.value"-->
<!--            >{{dict.label}}</el-radio>-->
<!--          </el-radio-group>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitBookForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { listHotel, getHotel, delHotel, addHotel, updateHotel } from "@/api/booking/hotel";
import {addBook} from "@/api/booking/book";
import { getid } from "@/api/booking/getid";
import { listScenicarea } from "@/api/tourss/scenicarea/area";

export default {
  name: "Hotel",
  dicts: ['hotel_star', 'service_yes_no','payment_status'],
  data() {
    return {
      scenicAreas: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 酒店信息表格数据
      hotelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openBook: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hotelName: null,
        hotelAddress: null,
        hotelType: null,
        areaId: null,
        price: null,
        breakfast: null,
        pickup: null
      },
      // 表单参数
      form: {paymentStatus: '0'},
      // 表单校验
      rules: {
        hotelName: [
          { required: true, message: "酒店名称不能为空", trigger: "blur" }
        ],
        hotelAddress: [
          { required: true, message: "酒店地址不能为空", trigger: "blur" }
        ],
        hotelType: [
          { required: true, message: "酒店星级不能为空", trigger: "change" }
        ],
        areaId: [
          { required: true, message: "所属景区不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "酒店一间房的价格不能为空", trigger: "blur" }
        ],
        breakfast: [
          { required: true, message: "含早餐不能为空", trigger: "change" }
        ],
        pickup: [
          { required: true, message: "接机服务不能为空", trigger: "change" }
        ]
      },
      hotelbookrules: {
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        hotelId: [
          { required: true, message: "酒店id不能为空", trigger: "blur" }
        ],
        checkinDate: [
          { required: true, message: "入住日期不能为空", trigger: "blur" }
        ],
        endDate: [
          { required: true, message: "退房日期不能为空", trigger: "blur" }
        ],
        guestCount: [
          { required: true, message: "人数不能为空", trigger: "blur" }
        ],
        roomCount: [
          { required: true, message: "需求房间数量不能为空", trigger: "blur" }
        ],
        totalPrice: [
          { required: true, message: "总价不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.fetchScenicAreas();
  },
  methods: {
    /** 查询酒店信息列表 */
    getList() {
      this.loading = true;
      listHotel(this.queryParams).then(response => {
        this.hotelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    fetchScenicAreas() {
      listScenicarea().then(response => {
        this.scenicAreas = response.rows; // 假设返回的数据结构中有 areaId 和 areaName 字段
      }).catch(error => {
        console.error('获取景区列表失败：', error);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openBook = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        hotelId: null,
        hotelName: null,
        hotelAddress: null,
        hotelType: null,
        areaId: null,
        price: null,
        breakfast: null,
        pickup: null
      };
      this.resetForm("form");
    },
    calculateTotalPrice(){
      if (this.form.roomCount && this.form.price) {
        this.form.totalPrice = this.form.roomCount * this.form.price;
      } else {
        this.form.totalPrice = null;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.hotelId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加酒店信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const hotelId = row.hotelId || this.ids
      getHotel(hotelId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改酒店信息";
      });
    },
    handleBook(row){
      this.form.hotelId = row.hotelId;
      this.form.price = row.price;
      // 调用 getid 接口获取 userId
      getid().then(response => {
        this.form.userId = response.data; // 假设返回的数据结构中有 userId 字段
        this.openBook = true;
        this.title = "预订酒店";
      }).catch(error => {
        console.error('获取 userId 失败：', error);
        // 处理获取 userId 失败的情况
      });
    },
    submitBookForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
            addBook(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openBook = false;
            });
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.hotelId != null) {
            updateHotel(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHotel(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const hotelIds = row.hotelId || this.ids;
      this.$modal.confirm('是否确认删除酒店信息编号为"' + hotelIds + '"的数据项？').then(function() {
        return delHotel(hotelIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('booking/hotel/export', {
        ...this.queryParams
      }, `hotel_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
